<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
         
<%@page import="POJO.Product"%>
<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="util.MybatisSession" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<script src="jquery.js?_=201703042022"></script>
<head>
<style type="text/css">
	.ad{
	text-align:center;
	margin: 30px 0 0 130px;
	border:0;
	padding:0;
	background: white;
	float: left;
	}
	
	
</style>

<script type="text/javascript">
	function addproduct(productId) {
		$.ajax({
			url: "AddProductServlet?pid=" + productId,
			method: "GET",
			dataType: "json",
			
			success: function(data) {				
					var num = data.num;
					$("div.head span").text(num);			
			}
		});
	}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
<%
	String statement = "mapper.productMapper.selectAllProducts";
	List<Product> products = MybatisSession.openSession().selectList(statement);	
%>
	<jsp:include page="top.jsp"></jsp:include>
	<%for (Product p : products) {%>
	<table>
	<tr>
	<td width="100px"><%=p.getName() %></td> 
	<td width="100px"><%=p.getCode() %></td> 
	<td width="100px"><%=p.getPrice() %></td>
	<td> <button onclick="addproduct(<%=p.getProductId()%>)">添加至购物车</button></td>
	</tr>

	</table>
	<%} %>




</body>
</html>